/*
 * @Author: aum
 * @Date: 2021-03-03 14:08:05
 * @LastEditTime: 2021-08-27 18:50:54
 * @LastEditors: aum
 * @Description: axios封装
 * @FilePath: \vue3-hand-vant\src\utils\http\index.ts
 */

import { AxiosRequestConfig, AxiosInstance, CustomOpts } from 'axios'
import { Get, Post } from '../type'
import { Interceptors } from './Interceptors'
import qs from 'qs'
class HttpService {
  private static instance: HttpService
  public axios: AxiosInstance
  public constructor() {
    this.axios = new Interceptors().getInterceptors()
  }

  public getInstance() {
    if (!HttpService.instance) {
      HttpService.instance = new HttpService()
    }
    return HttpService.instance
  }

  public get: Get = async <T>(url: string, params?: object, config?: AxiosRequestConfig) => {
    const response: any = await this.axios.get<T>(
      params ? `${url}?${qs.stringify(params)}` : url,
      config
    )
    return response
  }

  public post: Post = async <T>(
    url: string,
    params?: object,
    options?: CustomOpts,
    config?: AxiosRequestConfig
  ) => {
    const response: any = await this.axios.post<T>(url, params, config)
    return response
  }
}

export default new HttpService().getInstance()
